import time
from colors import *
from collections import defaultdict

#class Logger():
#    class Levels():
#        info = 'INFO'
#        fatal = 'FATAL'
#        error = 'ERROR'
#        debug = 'DEBUG'
#
#    logFormat = {
#        Levels.info: Colors.green,
#        Levels.fatal: Colors.red,
#        Levels.error: Colors.cyan,
#        Levels.debug: Colors.grey
#    }
#
#    def __init__(self):
#        self.disabledLevels = set()
#
#    def disableLevel(self, level):
#        self.disabledLevels.add(level)
#
#    def enableLevel(self, level):
#        if level in self.disabledLevels:
#            self.disabledLevels.remove(level)
#
#    def __printString(self, level, message):
#        sec = time.time()
#        ms = int((sec - int(sec)) * 1000)
#        print '[ {time} ][ {ms}ms ][ {level} ] {message}'.format(
#            time = time.strftime('%d/%m %H:%M:%S', time.localtime(sec)),
#            ms = ms,
#            level = self.logFormat.get(level) + level + Colors.clear,
#            message = message
#        )
#
#    def info(self, message):
#        self.__printString(self.Levels.info, message)
#
#    def fatal(self, message):
#        self.__printString(self.Levels.fatal, message)
#
#    def error(self, message):
#        self.__printString(self.Levels.error, message)
#
#    def debug(self, message):
#        self.__printString(self.Levels.debug, message)
#
#
#Log = Logger
#
#__all__ = [
#    'Log'
#]


class Logger:
    class __Log:
        class Levels:
            fatal = 'FATAL'
            error = 'ERROR'
            info = 'INFO'
            debug = 'DEBUG'

        logFormats = {
            Levels.fatal: Colors.red,
            Levels.error: Colors.cyan,
            Levels.info: Colors.green,
            Levels.debug: Colors.grey
        }

        def __init__(self, appName):
            self.appName = appName
        def __logString(self, level, logString):
            sec = time.time()
            ms = int((sec - int(sec)) * 1000)
            return '[ {time} ][ {ms}ms ][ {level} ] [ {appName} ] {message}'.format(
                time=time.strftime('%d/%m %H:%M:%S', time.localtime(sec)),
                ms=ms,
                level=self.logFormats.get(level) + level + Colors.clear,
                message=logString,
                appName=self.appName
            )
        def __write(self, logString):
            print logString
        def info(self, message):
            self.__write(self.__logString(self.Levels.info, message))
        def debug(self, message):
            self.__write(self.__logString(self.Levels.debug, message))
        def fatal(self, message):
            self.__write(self.__logString(self.Levels.fatal, message))
        def error(self, message):
            self.__write(self.__logString(self.Levels.error, message))

    def __init__(self):
        self.apps = defaultdict(dict)

    def constructLog(self, appName):
        log = self.__Log(appName=appName)
        self.apps[appName] = log
        return log

logger = Logger()

__all__ = [
    'logger'
]